package com.ruoyi.web.controller.system;

import com.ruoyi.web.service.C2PublishSucRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/C2PublishSucRate")
public class C2PublishSucRateController {

    @Autowired
    private C2PublishSucRateService c2PubSucRateService;

    /**
     * 获取每个自然月周期内的统计数据（pubStatus 字段）
     *
     * @return 返回包含时间周期、总记录数、pubStatus 为 1 的次数和百分比的数据
     */
    @GetMapping("/byMonths")
    public ResponseEntity<Map<String, Object>> getC2PubStatusRateByMonths() {
        try {
            // 调用服务层方法获取统计数据
            Map<String, Object> response = c2PubSucRateService.getC2PubStatusRateByMonths();

            // 如果返回数据为空，返回 HTTP 状态码 204
            if (response == null || response.isEmpty()) {
                return ResponseEntity.status(HttpStatus.NO_CONTENT).body(Collections.emptyMap());
            }

            // 正常返回结果
            return ResponseEntity.ok(response);

        } catch (Exception e) {
            // 捕获异常并返回错误信息
            e.printStackTrace();

            // 构造错误信息并返回
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("error", "Data fetching failed: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

    @GetMapping("/byWeeks")
    public ResponseEntity<Map<String, Object>> getC2PubStatusRateByWeeks() {
        try {
            // 调用服务层方法获取统计数据
            Map<String, Object> response = c2PubSucRateService.getC2PubStatusRateByWeeks();

            // 如果返回数据为空，返回 HTTP 状态码 204
            if (response == null || response.isEmpty()) {
                return ResponseEntity.status(HttpStatus.NO_CONTENT).body(Collections.emptyMap());
            }

            // 正常返回结果
            return ResponseEntity.ok(response);

        } catch (Exception e) {
            // 捕获异常并返回错误信息
            e.printStackTrace();

            // 构造错误信息并返回
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("error", "Data fetching failed: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }


    /**
     * 获取每个自然季度周期内的统计数据（pubStatus 字段）
     *
     * @return 返回包含季度、总记录数、pubStatus 为 0 的次数和百分比的数据
     */
    @GetMapping("/byQuarters")
    public ResponseEntity<Map<String, Object>> getC2PubStatusRateByQuarters() {
        try {
            // 调用服务层方法获取按季度统计的数据
            Map<String, Object> response = c2PubSucRateService.getC2PubStatusRateByQuarters();

            // 如果返回数据为空，返回 HTTP 状态码 204（没有内容）
            if (response == null || response.isEmpty()) {
                return ResponseEntity.status(HttpStatus.NO_CONTENT).body(Collections.emptyMap());
            }

            // 正常返回结果
            return ResponseEntity.ok(response);

        } catch (Exception e) {
            // 捕获异常并返回错误信息
            e.printStackTrace();

            // 构造错误信息并返回
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("error", "Data fetching failed: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }



    @GetMapping("/byYears")
    public ResponseEntity<Map<String, Object>> getC2PubStatusRateByYears() {
        try {
            // 调用服务层方法获取按年度统计的数据
            Map<String, Object> response = c2PubSucRateService.getC2PubStatusRateByYears();

            // 如果返回数据为空，返回 HTTP 状态码 204（没有内容）
            if (response == null || response.isEmpty()) {
                return ResponseEntity.status(HttpStatus.NO_CONTENT).body(Collections.emptyMap());
            }

            // 正常返回结果
            return ResponseEntity.ok(response);

        } catch (Exception e) {
            // 捕获异常并返回错误信息
            e.printStackTrace();

            // 构造错误信息并返回
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("error", "Data fetching failed: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

}
